﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace HZ.DrugTrade.Web.Admin.RelationQuery
{
    public partial class RelationCheckAreaByMS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CurrentAccount.GetUser().UserID.ToUpper().Contains("ZBZX"))
            {
                this.Panel1.Visible = false;
            }
            if (!IsPostBack)
            {
                BindCom();
            }
        }
        //绑定GridView
        public void BindCom()
        {
            
            string userId = CurrentAccount.GetUser().UserID;
            string comName = HZ.Fun.CheckString(this.txtCompanyName.Text.Trim());
            string where = "";
            if (userId.ToUpper().Contains("ZBZX"))//若是中心
            {
                this.gvwArea.Columns[0].Visible = true;
                //and checkByZbzx =1 中心审核状态
                where = string.Format(" and CompanyName like '%{0}%' ", comName);
                //where = string.Format(" and CompanyName like '%{1}%'",comName);
            }
            else//若是盟市
            {
                //this.gvwArea.Columns[0].Visible = false;
                //and Unioncitycheckstatus = 1 盟市审核配送企业信息状态
                //and checkByZbzx =1 中心审核状态
                where = string.Format(" and ManageOrgID='{0}' and CompanyName like '%{1}%'", userId, comName);
            }
            if (!string.IsNullOrEmpty(ddlMSStatus.SelectedValue))
            {
                where += " and checkstatus="+ddlMSStatus.SelectedValue+"";
            }
            if (!string.IsNullOrEmpty(ddlZXStatus.SelectedValue))
            {
                where += " and checkbyzbzx="+ddlZXStatus.SelectedValue+"";
            }
            switch (ddlProjectid.SelectedValue)
            {
                case "0001":
                    where += " and ishcygcg is null";
                    break;
                case "0002":
                    where += " and ishcygcg = 1";
                    break;
                default:
                    break;
            }
            where += " order by companyps_id,areaid";
            int totalRecord = 0;
            this.gvwArea.DataSource = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().GetComByIdMS(where, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalRecord);
            this.AspNetPager1.RecordCount = totalRecord;
            this.gvwArea.DataBind();
        }
        //审核通过
        protected void btnCheck_Click(object sender, EventArgs e)
        {
            string areaIds = "";
            string comIds = "";
            //遍历列表的所有行
            for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
            {
                CheckBox ckb = ((CheckBox)gvwArea.Rows[_index].FindControl("checkbox1"));
                if (ckb.Checked)
                {
                    comIds += "'" + this.gvwArea.DataKeys[_index][0].ToString() + "'" + ",";
                    areaIds += "'" + this.gvwArea.DataKeys[_index][1].ToString() + "'" + ",";
                }
            }
            if (comIds != "")
            {
                bool flag = false;
                if (CurrentAccount.GetUser().UserID.ToUpper().Contains("ZBZX"))
                {
                    flag = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().CheckAllEnterPrise(comIds.Substring(0, comIds.Length - 1), areaIds.Substring(0, areaIds.Length - 1), 1);
                }
                else
                {
                    flag = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().CheckEnterPrise(comIds.Substring(0, comIds.Length - 1), 1, CurrentAccount.GetUser().UserID);
                }

                if (flag)
                {
                    this.Controls.Add(new LiteralControl("<script>alert('审核成功');</script>"));
                    BindCom();
                }
                else
                {
                    HZ.Web.WebUtil.Alert("审核失败", this);
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("请至少选择一项", this);
            }
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindCom();
        }

        protected void btnInquery_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindCom();
        }
        //审核不通过
        protected void btnCheckNo_Click(object sender, EventArgs e)
        {
            string areaIds = "";
            string comIds = "";
            //遍历列表的所有行
            for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
            {
                CheckBox ckb = ((CheckBox)gvwArea.Rows[_index].FindControl("checkbox1"));
                if (ckb.Checked)
                {
                    comIds += "'" + this.gvwArea.DataKeys[_index][0].ToString() + "'" + ",";
                    areaIds += "'" + this.gvwArea.DataKeys[_index][1].ToString() + "'" + ",";
                }
            }
            if (comIds != "")
            {
                bool flag = false;
                if (CurrentAccount.GetUser().UserID.ToUpper().Contains("ZBZX"))
                {
                    flag = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().CheckAllEnterPrise(comIds.Substring(0, comIds.Length - 1), areaIds.Substring(0, areaIds.Length - 1), 2);
                }
                else
                {
                    flag = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().CheckEnterPrise(comIds.Substring(0, comIds.Length - 1), 2, CurrentAccount.GetUser().UserID);
                }
                if (flag)
                {
                    this.Controls.Add(new LiteralControl("<script>alert('审核成功');</script>"));
                    BindCom();
                }
                else
                {
                    HZ.Web.WebUtil.Alert("审核失败", this);
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("请至少选择一项", this);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            string userId = CurrentAccount.GetUser().UserID;
            string comName = HZ.Fun.CheckString(this.txtCompanyName.Text.Trim());
            string where = "";
            if (userId.ToUpper().Contains("ZBZX"))//若是中心
            {
                this.gvwArea.Columns[0].Visible = true;
                where = string.Format(" and CompanyName like '%{0}%' and checkByZbzx =1", comName);
                //where = string.Format(" and CompanyName like '%{1}%'",comName);
            }
            else//若是盟市
            {
                //this.gvwArea.Columns[0].Visible = false;
                where = string.Format(" and Unioncitycheckstatus = 1 and checkByZbzx =1 and ManageOrgID='{0}' and CompanyName like '%{1}%'", userId, comName);
            }
            if (!string.IsNullOrEmpty(ddlMSStatus.SelectedValue))
            {
                where += " and checkstatus=" + ddlMSStatus.SelectedValue + "";
            }
            if (!string.IsNullOrEmpty(ddlZXStatus.SelectedValue))
            {
                where += " and checkbyzbzx=" + ddlZXStatus.SelectedValue + "";
            }
            switch (ddlProjectid.SelectedValue)
            {
                case "0001":
                    where += " and ishcygcg is null";
                    break;
                case "0002":
                    where += " and ishcygcg = 1";
                    break;
                default:
                    break;
            }
            where += " order by companyps_id,areaid";
            int totalRecord = 0;
            DataTable dt = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().GetComByIdMS(where, 1,0, out totalRecord);
            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvwArea);
        }
    }
}
